package com.neusoft.elmboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.service.BusinessService;

import jakarta.servlet.http.HttpSession;

@RestController
@RequestMapping("/BusinessController")
public class BusinessController {

	@Autowired
	private BusinessService businessService;

	@RequestMapping("/listBusinessByOrderTypeId")
	public List<Business> listBusinessByOrderTypeId(Business business) throws Exception {
		return businessService.listBusinessByOrderTypeId(business.getOrderTypeId());
	}

	@RequestMapping("/getBusinessById")
	public Business getBusinessById(Business business) throws Exception {
		return businessService.getBusinessById(Math.toIntExact(business.getBusinessId()));
	}

	@RequestMapping("/getCurrentBusiness")
	public ResponseEntity<?> getCurrentBusiness(HttpSession session) {
		System.out.println("getCurrentBusiness method called");
		Long userIdLong = (Long) session.getAttribute("userId");
		String userId = userIdLong != null ? String.valueOf(userIdLong) : null;

		Business currentBusiness = businessService.getCurrentBusiness(userId);
		if (currentBusiness != null) {
			return ResponseEntity.ok(currentBusiness);
		} else {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("商家信息未找到");
		}
	}


}
